package com.example.monit.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.Random;
import java.util.UUID;

public class CityUtils {

    public static LinkedList<String> list = new LinkedList<>();
    public static LinkedList<String> userId = new LinkedList<>();
    public static LinkedList<String> browsers = new LinkedList<>();
    public static Random random = new Random();

    static {
        byte[] bytes;
        try {
            bytes = Files.readAllBytes(Paths.get("D:\\development\\java\\monitoring\\src\\main\\resources\\static\\json\\data-city.json"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String s = new String(bytes, StandardCharsets.UTF_8);
        JSONObject jsonObject = JSON.parseObject(s);
        JSONArray features = jsonObject.getJSONArray("features");
        int size = features.size();
        for (int i = 0; i < size; i++) {
            JSONObject properties = features.getJSONObject(i).getJSONObject("properties");
            String name = properties.getString("name");
            list.add(name);
        }
        for (int i = 0; i < 100; i++) {
            String id1 = UUID.randomUUID().toString().replaceAll("-", "");
            userId.add(id1);
        }

        browsers.add("IE8-");
        browsers.add("IE9+");
        browsers.add("Safari");
        browsers.add("Firefox");
        browsers.add("Firefox");
        browsers.add("Chrome");
    }

    public static String getCity() {
        String s = list.get(random.nextInt(list.size()));
        return s;
    }

    public static String getUserId() {
        String s = userId.get(random.nextInt(userId.size()));
        return s;
    }

    public static String getBrowser() {
        String s = browsers.get(random.nextInt(browsers.size()));
        return s;
    }
}
